package com.imooc.oa.controller;

import com.alibaba.fastjson.JSON;
import com.imooc.oa.entity.User;
import com.imooc.oa.service.UserService;
import com.imooc.oa.service.exception.BussinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
//登陆验证控制器
@WebServlet(name = "LoginServlet",urlPatterns="/check_login")
public class LoginServlet extends HttpServlet {
    Logger logger= LoggerFactory.getLogger(LoginServlet.class);//日志对象
    private UserService userService=new UserService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置接收和响应的字符集
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //接收用户输入
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        Map<String,Object> result=new HashMap<>();
        try {
            //调用业务逻辑
            User user=userService.checkLogin(username, password);
            HttpSession session=request.getSession();
            //向HttpSession存入登录用户信息，属性名："login_user"
            session.setAttribute("login_user", user);
            result.put("code","0");
            result.put("message","success");
            //放入重定向的路径键值对
            result.put("redirect_url","/index");
        }catch(BussinessException e)
        {
            //异常抛出
            logger.error(e.getMessage(),e);
            result.put("code", e.getCode());
            result.put("message",e.getMessage());



        }
        catch (Exception e) {
            logger.error(e.getMessage(),e);
            result.put("code",e.getClass().getSimpleName());
            result.put("message",e.getMessage());
        }
        //返回对应结果
        String json= JSON.toJSONString(result);
        response.getWriter().println(json);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
